import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import {
  NzFormatEmitEvent,
  NzTreeNodeOptions,
  NzTreeComponent,
  isTemplateRef,
} from 'ng-zorro-antd';
import { CommonService } from '../../../core/service/common.service';
import { SysTypeTypeModalComponent } from './type-modal/type-modal.component';

@Component({
  selector: 'app-sys-type',
  templateUrl: './type.component.html',
})
export class SysTypeComponent implements OnInit {
  url = '';
  item: any = {};
  @ViewChild('tree') tree: NzTreeComponent;

  nodes: any[] = [];

  nzClick(event: NzFormatEmitEvent): void {
    if('click' == event.eventName){
      this.item = event.node;
    }
  }

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private commonService: CommonService,
  ) {}

  ngOnInit() {
    // this.url = `${this.commonService.getServerPath()}api/sysTypeTree`;
    this.url = 'assets/data/sysTypeTree.json';
    this.loadTree();
  }

  loadTree() {
    let me = this;
    me.http.get(this.url).subscribe((succ: any) => {
      me.nodes = succ;
    });
  }

  add() {
    this.modal
      .createStatic(SysTypeTypeModalComponent, { i: {pid: this.item.key || ''} })
      .subscribe(() => {
        this.loadTree();
      });
  }
}
